package Form;

import Control.CommonControls;
import Control.MySQLController;
import Control.NeedyResident_UpdateUser_Control;
import Control.User_CommonControls;
import Entity.NeedyResident_UpdateAccount;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;


public class NeedyResident_UpdateUser_Form extends JPanel{
	
	NeedyResident_UpdateUser_Control controller;
	public static JDialog dialog;
	
	private JLabel lblNric;
	private JLabel lblPassword;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_6;
	private JLabel lblFamilySize;
	private JLabel lbl_nric;
	private JLabel lbl_name;
	private JLabel lbl_dob;
	private JTextField tf_contactNo;
	private JTextField tf_email;
	private JTextField tf_address;
	private JTextField tf_monthlyIncome;
	private JTextField tf_familySize;
	private JButton btn_update;
	private JLabel lblNewLabel_5;
	private JPasswordField pf_newpassword;
	private JPasswordField pf_oldpassword;
	private JButton btnDeleteAccount;
	private String Password;
	private String MonthlyIncome;
	private String FamilySize;
	
	public NeedyResident_UpdateUser_Form() {
		
		controller = new NeedyResident_UpdateUser_Control(this);
		

		
		//To get password and other things from Database.
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user INNER JOIN ooadpj.needy_resident ON user.nric = needy_resident.nric WHERE needy_resident.nric= '" + CommonControls.getUser().getNric() + "'";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Password = rs.getString("password");
				MonthlyIncome = rs.getString("monthly_income");
				FamilySize = rs.getString("family_size");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();

	
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblNric = new GridBagConstraints();
		gbc_lblNric.insets = new Insets(0, 0, 5, 5);
		gbc_lblNric.gridx = 1;
		gbc_lblNric.gridy = 1;
		add(getLblNric(), gbc_lblNric);
		GridBagConstraints gbc_lbl_nric = new GridBagConstraints();
		gbc_lbl_nric.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lbl_nric.insets = new Insets(0, 0, 5, 0);
		gbc_lbl_nric.gridx = 2;
		gbc_lbl_nric.gridy = 1;
		add(getLbl_nric(), gbc_lbl_nric);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 2;
		add(getLblNewLabel(), gbc_lblNewLabel);
		GridBagConstraints gbc_lbl_name = new GridBagConstraints();
		gbc_lbl_name.anchor = GridBagConstraints.WEST;
		gbc_lbl_name.insets = new Insets(0, 0, 5, 0);
		gbc_lbl_name.gridx = 2;
		gbc_lbl_name.gridy = 2;
		add(getLbl_name(), gbc_lbl_name);
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 1;
		gbc_lblNewLabel_3.gridy = 3;
		add(getLblNewLabel_3(), gbc_lblNewLabel_3);
		GridBagConstraints gbc_lbl_dob = new GridBagConstraints();
		gbc_lbl_dob.anchor = GridBagConstraints.WEST;
		gbc_lbl_dob.insets = new Insets(0, 0, 5, 0);
		gbc_lbl_dob.gridx = 2;
		gbc_lbl_dob.gridy = 3;
		add(getLbl_dob(), gbc_lbl_dob);
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 1;
		gbc_lblNewLabel_2.gridy = 4;
		add(getLblNewLabel_2(), gbc_lblNewLabel_2);
		GridBagConstraints gbc_tf_contactNo = new GridBagConstraints();
		gbc_tf_contactNo.insets = new Insets(0, 0, 5, 0);
		gbc_tf_contactNo.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_contactNo.gridx = 2;
		gbc_tf_contactNo.gridy = 4;
		add(getTf_contactNo(), gbc_tf_contactNo);
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 1;
		gbc_lblNewLabel_1.gridy = 5;
		add(getLblNewLabel_1(), gbc_lblNewLabel_1);
		GridBagConstraints gbc_tf_email = new GridBagConstraints();
		gbc_tf_email.insets = new Insets(0, 0, 5, 0);
		gbc_tf_email.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_email.gridx = 2;
		gbc_tf_email.gridy = 5;
		add(getTf_email(), gbc_tf_email);
		GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
		gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_4.gridx = 1;
		gbc_lblNewLabel_4.gridy = 6;
		add(getLblNewLabel_4(), gbc_lblNewLabel_4);
		GridBagConstraints gbc_tf_address = new GridBagConstraints();
		gbc_tf_address.insets = new Insets(0, 0, 5, 0);
		gbc_tf_address.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_address.gridx = 2;
		gbc_tf_address.gridy = 6;
		add(getTf_address(), gbc_tf_address);
		GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
		gbc_lblNewLabel_6.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_6.gridx = 1;
		gbc_lblNewLabel_6.gridy = 7;
		add(getLblNewLabel_6(), gbc_lblNewLabel_6);
		GridBagConstraints gbc_tf_monthlyIncome = new GridBagConstraints();
		gbc_tf_monthlyIncome.insets = new Insets(0, 0, 5, 0);
		gbc_tf_monthlyIncome.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_monthlyIncome.gridx = 2;
		gbc_tf_monthlyIncome.gridy = 7;
		add(getTf_monthlyIncome(), gbc_tf_monthlyIncome);
		GridBagConstraints gbc_lblFamilySize = new GridBagConstraints();
		gbc_lblFamilySize.insets = new Insets(0, 0, 5, 5);
		gbc_lblFamilySize.gridx = 1;
		gbc_lblFamilySize.gridy = 8;
		add(getLblFamilySize(), gbc_lblFamilySize);
		GridBagConstraints gbc_tf_familySize = new GridBagConstraints();
		gbc_tf_familySize.insets = new Insets(0, 0, 5, 0);
		gbc_tf_familySize.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_familySize.gridx = 2;
		gbc_tf_familySize.gridy = 8;
		add(getTf_familySize(), gbc_tf_familySize);
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = 9;
		add(getLblPassword(), gbc_lblPassword);
		GridBagConstraints gbc_pf_newpassword = new GridBagConstraints();
		gbc_pf_newpassword.insets = new Insets(0, 0, 5, 0);
		gbc_pf_newpassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pf_newpassword.gridx = 2;
		gbc_pf_newpassword.gridy = 9;
		add(getPf_newpassword(), gbc_pf_newpassword);
		GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
		gbc_lblNewLabel_5.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_5.gridx = 1;
		gbc_lblNewLabel_5.gridy = 10;
		add(getLblNewLabel_5(), gbc_lblNewLabel_5);
		GridBagConstraints gbc_pf_oldpassword = new GridBagConstraints();
		gbc_pf_oldpassword.insets = new Insets(0, 0, 5, 0);
		gbc_pf_oldpassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pf_oldpassword.gridx = 2;
		gbc_pf_oldpassword.gridy = 10;
		add(getPf_oldpassword(), gbc_pf_oldpassword);
		GridBagConstraints gbc_btnDeleteAccount = new GridBagConstraints();
		gbc_btnDeleteAccount.insets = new Insets(0, 0, 0, 5);
		gbc_btnDeleteAccount.gridx = 1;
		gbc_btnDeleteAccount.gridy = 11;
		add(getBtnDeleteAccount(), gbc_btnDeleteAccount);
		GridBagConstraints gbc_btn_update = new GridBagConstraints();
		gbc_btn_update.gridx = 2;
		gbc_btn_update.gridy = 11;
		add(getBtn_update(), gbc_btn_update);
	}

	public JLabel getLblNric() {
		if (lblNric == null) {
			lblNric = new JLabel("Nric");
		}
		return lblNric;
	}
	public JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel("New Password");
		}
		return lblPassword;
	}
	public JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Name");
		}
		return lblNewLabel;
	}
	public JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Email");
		}
		return lblNewLabel_1;
	}
	public JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("Contact Number");
		}
		return lblNewLabel_2;
	}
	public JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("Date of Birth");
		}
		return lblNewLabel_3;
	}
	public JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("Address");
		}
		return lblNewLabel_4;
	}
	public JLabel getLblNewLabel_6() {
		if (lblNewLabel_6 == null) {
			lblNewLabel_6 = new JLabel("Monthly Income");
		}
		return lblNewLabel_6;
	}
	public JLabel getLblFamilySize() {
		if (lblFamilySize == null) {
			lblFamilySize = new JLabel("Family Size");
		}
		return lblFamilySize;
	}
	public JLabel getLbl_nric() {
		if (lbl_nric == null) {
			lbl_nric = new JLabel(CommonControls.getUser().getNric());
		}
		return lbl_nric;
	}
	public JLabel getLbl_name() {
		if (lbl_name == null) {
			lbl_name = new JLabel(CommonControls.getUser().getName());
		}
		return lbl_name;
	}
	public JLabel getLbl_dob() {
		if (lbl_dob == null) {
			lbl_dob = new JLabel(CommonControls.getUser().getDob().toString());
		}
		return lbl_dob;
	}
	public JTextField getTf_contactNo() {
		if (tf_contactNo == null) {
			tf_contactNo = new JTextField(Integer.toString(CommonControls.getUser().getContact_no()));
			tf_contactNo.setColumns(10);
		}
		return tf_contactNo;
	}
	public JTextField getTf_email() {
		if (tf_email == null) {
			tf_email = new JTextField(CommonControls.getUser().getEmail());
			tf_email.setColumns(10);
		}
		return tf_email;
	}
	public JTextField getTf_address() {
		if (tf_address == null) {
			tf_address = new JTextField(CommonControls.getUser().getAddress());
			tf_address.setColumns(10);
		}
		return tf_address;
	}
	public JTextField getTf_monthlyIncome() {
		if (tf_monthlyIncome == null) {
			tf_monthlyIncome = new JTextField(MonthlyIncome);
			tf_monthlyIncome.setColumns(10);
		}
		return tf_monthlyIncome;
	}
	public JTextField getTf_familySize() {
		if (tf_familySize == null) {
			tf_familySize = new JTextField(FamilySize);
			tf_familySize.setColumns(10);
		}
		return tf_familySize;
	}
	public JButton getBtn_update() {
		if (btn_update == null) {
			btn_update = new JButton("Update");
			final NeedyResident_UpdateUser_Form panel = this;
			btn_update.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (CommonControls.validatePanelFields(panel) == true){
						if ((User_CommonControls.ValidationContactNo(getTf_contactNo().getText().toString())) == true){
							if ((User_CommonControls.ValidationMonthlyIncome(Double.parseDouble(getTf_monthlyIncome().getText()))) == true){
								if ((User_CommonControls.ValidationFamilySize(Integer.parseInt(getTf_familySize().getText()))) == true){
									if ((new String(getPf_oldpassword().getPassword())).equals(Password)){
										controller.UpdateUser();
									}
									else
										JOptionPane.showMessageDialog(null, "Old Passwords do not match");
								}
								else
									JOptionPane.showMessageDialog(null, "Family Size is invalid");
							}
							else
								JOptionPane.showMessageDialog(null, "Monthly Income is not within $0 to $3000");
							
						}
						else
							JOptionPane.showMessageDialog(null, "Contact number is not valid");
					}
					
				}
			});
		}
		return btn_update;
	}
	public JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("Old Password");
		}
		return lblNewLabel_5;
	}
	public JPasswordField getPf_newpassword() {
		if (pf_newpassword == null) {
			pf_newpassword = new JPasswordField();
		}
		return pf_newpassword;
	}
	public JPasswordField getPf_oldpassword() {
		if (pf_oldpassword == null) {
			pf_oldpassword = new JPasswordField();
		}
		return pf_oldpassword;
	}
	public JButton getBtnDeleteAccount() {
		if (btnDeleteAccount == null) {
			btnDeleteAccount = new JButton("Delete Account");
			btnDeleteAccount.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dialog = new JDialog(Main.frame, true);
					dialog.add(new NeedyResident_DeleteUser_Form());
					
					dialog.pack();
					dialog.setLocationRelativeTo(Main.frame);
					dialog.setVisible(true);
					
				}
			});
		}
		return btnDeleteAccount;
	}
	
	
	
}